﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq.Expressions;
using System.IO;
namespace RAMED
{
    public partial class RechercheForm : Form
    {


        ModifierpvForm MPF = new ModifierpvForm();
        private List<Pv> pvsRecherches = new List<Pv>();
        public RechercheForm()
        {
            InitializeComponent();
            RAMEDEntities1 context = SingletonContext.GetContext();
            comboannexe.DataSource = context.Annexe_administratif;
            comboannexe.DisplayMember = "nom_annexe_administratif";
            comboannexe.SelectedItem = null;
        }




        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }


        private void txtparcin_TextChanged(object sender, EventArgs e)
        {
            //DataView dv = new DataView(dv.beneficiaire);
            //dv.RowFilter = string.Format("CIN_beneficiaire like '%{0}%'" + txtparCIN.Text);
            //dataGridView1.DataSource = dv;


        }

        private void dateTimepickerpardate_ValueChanged(object sender, EventArgs e)
        {

        }



        private void RechercheForm_Load(object sender, EventArgs e)
        {

        }

        public void mettreAjourFormGridView()
        {
            dataGridViewrecherche.DataSource = null;
            remplirDataGridView();
            dataGridViewrecherche.Update();
            dataGridViewrecherche.Refresh();
        }



        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void comboannexe_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void RechercherBtn_Click(object sender, EventArgs e)
        {

            RAMEDEntities1 context = SingletonContext.GetContext();
            List<Pv> lesPvs = new List<Pv>();
            lesPvs = context.Pvs.ToList<Pv>();
            if (checkBoxdateinf.Checked)
            {
                lesPvs = lesPvs.Where(p => p.date_pv.Value.Date >= dateTimePickerdateInf.Value.Date).ToList<Pv>();
            }
            if (checkBoxdateSup.Checked)
            {
                lesPvs = lesPvs.Where(p => p.date_pv.Value <= dateTimePickerdateSup.Value).ToList<Pv>();
            }
            if (!txtCIN.Text.Trim().Equals(""))
            {
                Beneficaire beneficiare = new Beneficaire();
                beneficiare = context.Beneficaires.Where(p => p.CIN_beneficaire.Equals(txtCIN.Text)).First();
                lesPvs = lesPvs.Where(p => p.Beneficaires.Contains(beneficiare)).ToList<Pv>();
            }

            if (comboannexe.SelectedItem != null)
            {
                Annexe_administratif aa = new Annexe_administratif();
                aa = (Annexe_administratif)comboannexe.SelectedValue;
                lesPvs = lesPvs.Where(p => p.id_annexe_administratif == aa.id_annexe_administratif).ToList<Pv>();
            }
            pvsRecherches = lesPvs;
            mettreAjourFormGridView();

        }

        private void dateTimePickerfin_ValueChanged(object sender, EventArgs e)
        {

        }

        private void checkBoxcin_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btnmodifierpv_Click(object sender, EventArgs e)
        {
            if (dataGridViewrecherche.CurrentRow == null)
            {
                NotificationTest.NotificationManager nm = new NotificationTest.NotificationManager();
                nm.Show(this, "Veuillez sélectionner un enregistrement pour le modifier", Color.Red, 3000);
            }
            else
            {

                RAMEDEntities1 Context = SingletonContext.GetContext();
                Pv pl = new Pv();
                int id_pv = ((Pv)dataGridViewrecherche.CurrentRow.DataBoundItem).id_pv;
                pl = Context.Pvs.Where(d => d.id_pv == id_pv).First();
                MPF = new ModifierpvForm();
                MPF.PL = pl;
                MPF.RechercheFormParent = this;
                MPF.InitialiserChamps();
                MPF.Show();
            }
         }
        

        

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }


        private void remplirDataGridView()
        {
            dataGridViewrecherche.AutoGenerateColumns = false;
            dataGridViewrecherche.ColumnCount = 6;
            dataGridViewrecherche.Columns[0].HeaderText = "Date";
            dataGridViewrecherche.Columns[0].DataPropertyName = "date_pv";
            dataGridViewrecherche.Columns[0].Width = 180;

            dataGridViewrecherche.Columns[1].HeaderText = "Nombre de dossier";
            dataGridViewrecherche.Columns[1].DataPropertyName = "nombre_de_dossier";
            dataGridViewrecherche.Columns[1].Width = 180;

            dataGridViewrecherche.Columns[2].HeaderText = "Nombre de perssone sant pauvre";
            dataGridViewrecherche.Columns[2].DataPropertyName = "nombre_de_personne_sont_en_pauvre";
            dataGridViewrecherche.Columns[2].Width = 180;

            dataGridViewrecherche.Columns[3].HeaderText = "nombre de personne sont en vulnérabilité";
            dataGridViewrecherche.Columns[3].DataPropertyName = "nombre_de_personne_sont_en_vulnérabilité";
            dataGridViewrecherche.Columns[3].Width = 180;

            dataGridViewrecherche.Columns[4].HeaderText = "nombre acceptaion de dossier";
            dataGridViewrecherche.Columns[4].DataPropertyName = "nombre_acceptaion_de_dossie";
            dataGridViewrecherche.Columns[4].Width = 180;

            dataGridViewrecherche.Columns[5].HeaderText = "Annexe Administrative";
            dataGridViewrecherche.Columns[5].DataPropertyName = "Annexe_administratif";
            dataGridViewrecherche.Columns[5].Width = 180;

            
            dataGridViewrecherche.DataSource = pvsRecherches;
        }

        private void Button21_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

    }    
    }


